//
//  NSDictionary+Extension.swift
//  DoctorEPlus
//
//  Created by 王林波 on 2020/10/27.
//

import Foundation

extension NSDictionary {
    func toJsonString() -> String {
       if (!JSONSerialization.isValidJSONObject(self)) {
            print("无法解析出JSONString")
            return ""
        }
        let data : NSData! = try? JSONSerialization.data(withJSONObject: self, options: []) as NSData?
        let JSONString = NSString(data:data as Data,encoding: String.Encoding.utf8.rawValue)
        return JSONString! as String
    }
}

// 网络结果解析
extension NSDictionary {
    var success: Bool {
        get {
            let success = BHJsonTool.decodeIntFromDic(dic: self as? Dictionary<String, Any>, key: "success")
            if success == 1 {
                return true
            }else {
                return false
            }
        }
    }
    
    var message: String {
        get {
            let message = BHJsonTool.decodeStringFromDic(dic: self as? Dictionary<String, Any>, key: "message")
            return message
        }
    }
    
    
    var code: Int {
        get {
            let code = BHJsonTool.decodeIntFromDic(dic: self as? Dictionary<String, Any>, key: "code")
            return code
        }
    }
    
    var result: [String: Any] {
        get {
            let dic = BHJsonTool.decodeDicFromDic(dic: self as? Dictionary<String, Any>, key: "result")
            return dic
        }
    }
    
    
    var timestamp: Int {
        get {
            let timestamp = BHJsonTool.decodeIntFromDic(dic: self as? Dictionary<String, Any>, key: "timestamp")
            return timestamp
        }
    }
}
